package com.yuerlive.rainbow.module.main.fragment;

import android.Manifest;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.base.BaseFragment;
import com.yuerlive.rainbow.entity.ShareInfo;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.module.main.MainActivity;
import com.yuerlive.rainbow.module.main.presenter.MineContract;
import com.yuerlive.rainbow.module.main.presenter.MinePresenter;
import com.yuerlive.rainbow.module.mine.FeedbackActivity;
import com.yuerlive.rainbow.module.mine.MyAttentionActivity;
import com.yuerlive.rainbow.module.mine.MyFansActivity;
import com.yuerlive.rainbow.module.mine.MyInfoActivity;
import com.yuerlive.rainbow.module.mine.MyMessageActivity;
import com.yuerlive.rainbow.module.mine.MySettingActivity;
import com.yuerlive.rainbow.module.recharge.RechargeActivity;
import com.yuerlive.rainbow.module.web.WebViewActivity;
import com.yuerlive.rainbow.observer.Observerable;
import com.yuerlive.rainbow.observer.ObserverableType;
import com.yuerlive.rainbow.utils.FormatUtil;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.ShareUtils;
import com.yuerlive.rainbow.utils.UserProxy;
import com.yuerlive.rainbow.utils.Utils;
import com.yuerlive.rainbow.widget.ShareDialog;
import com.yuerlive.rainbow.widget.pullzoomscrollview.PullToZoomScrollViewEx;
import com.yuerlive.rainbow.wxapi.WXEntryActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2017/5/18.
 */

public class FragmentMine extends BaseFragment<MineContract.Presenter> implements View.OnClickListener,
        UserProxy.OnUserChangeListener, Observerable.ISubscribe, MineContract.View {
    @BindView(R.id.pzScrollView)
    PullToZoomScrollViewEx pzScrollView;
    ImageView ivBack, ivMessage, ivShare, ivEdite, ivZoom;
    TextView tvTitle, tvName, tvId, tvLevel, tvFans, tvCharms, tvFollowed;
    LinearLayout llGrade, llGold, llSetting, llFeedback, llFollow, llFans, llCharm,llInviteFriends;
    int[] iconResIDs;
    String[] itemTitles;
    String[] itemHints;
    private ShareDialog shareDialog;
    private User user = UserProxy.getUser();
    private ShareInfo info;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initViews(View root) {
        presenter = new MinePresenter(this);
        presenter.getUserInfo();
        UserProxy.addListener(this);
        initData();
        initPullZoomView();
        setOnClickListener();
        setData();
        Observerable.getInstance().subscribe(ObserverableType.ATTENTION, this);
    }


    private void setOnClickListener() {
        ivShare.setOnClickListener(this);
        ivEdite.setOnClickListener(this);
        ivMessage.setOnClickListener(this);

        llFeedback.setOnClickListener(this);
        llGold.setOnClickListener(this);
        llSetting.setOnClickListener(this);
        llGrade.setOnClickListener(this);
        llFans.setOnClickListener(this);
        llFollow.setOnClickListener(this);
        llCharm.setOnClickListener(this);
        llInviteFriends.setOnClickListener(this);
    }

    private void setData() {
            if (user == null || user.getUserInfo() == null ||tvId==null || tvName==null) {
                return;
            }
            tvId.setText("ID：" + user.getUserId());
            tvName.setText(user.getUserInfo().getNickname());
            if (user.getUserInfo().getSex() == 0) {
                tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_boy, 0);
            } else {
                tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon_girl, 0);
            }
            tvFans.setText(user.getUserInfo().getFans() + "");
            tvFollowed.setText(user.getUserInfo().getSubscribenum() + "");
            tvLevel.setText(user.getUserInfo().getLv() + "");
            Utils.setLevel(tvLevel, user.getUserInfo().getLv());
            tvCharms.setText(FormatUtil.formatCharmTotal(user.getUserInfo().getCharmtotalvalue())+"魅力值");
            GlideUtil.load(this.getContext(), user.getUserInfo().getIcon(), ivZoom);

    }


    private void initPullZoomView() {

        View headView = LayoutInflater.from(mActivity).inflate(R.layout.profile_head_view, null, false);
        ivBack = (ImageView) headView.findViewById(R.id.ivBack);
        tvTitle = (TextView) headView.findViewById(R.id.tvTitle);
        ivMessage = (ImageView) headView.findViewById(R.id.ivMessage);
        tvName = (TextView) headView.findViewById(R.id.tvName);
        tvId = (TextView) headView.findViewById(R.id.tvId);

        ivShare = (ImageView) headView.findViewById(R.id.ivShare);
        ivEdite = (ImageView) headView.findViewById(R.id.ivEdite);

        View zoomView = LayoutInflater.from(mActivity).inflate(R.layout.profile_zoom_view, null, false);
        ivZoom = (ImageView) zoomView.findViewById(R.id.ivZoom);

        View contentView = LayoutInflater.from(mActivity).inflate(R.layout.profile_content_view, null, false);
        llInviteFriends=(LinearLayout)contentView.findViewById(R.id.llInviteFriends);
        llFeedback = (LinearLayout) contentView.findViewById(R.id.llFeedback);
        llGold = (LinearLayout) contentView.findViewById(R.id.llGold);
        llGrade = (LinearLayout) contentView.findViewById(R.id.llGrade);
        llSetting = (LinearLayout) contentView.findViewById(R.id.llSetting);
        llFollow = (LinearLayout) contentView.findViewById(R.id.llFollowed);
        llCharm = (LinearLayout) contentView.findViewById(R.id.llCharm);
        llFans = (LinearLayout) contentView.findViewById(R.id.llFans);
        tvLevel = (TextView) contentView.findViewById(R.id.tvLevel);
        tvCharms = (TextView) contentView.findViewById(R.id.tvCharmNum);
        tvFans = (TextView) contentView.findViewById(R.id.tvFansNum);
        tvFollowed = (TextView) contentView.findViewById(R.id.tvFollowNum);

        pzScrollView.setHeaderView(headView);
        pzScrollView.setZoomView(zoomView);
        pzScrollView.setScrollContentView(contentView);
        pzScrollView.setParallax(false);
        pzScrollView.setZoomEnabled(true);
        DisplayMetrics localDisplayMetrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
        int mScreenWidth = localDisplayMetrics.widthPixels;
        LinearLayout.LayoutParams localObject = new LinearLayout.LayoutParams(mScreenWidth, mScreenWidth);
        pzScrollView.setHeaderLayoutParams(localObject);
        ButterKnife.bind(pzScrollView);
    }

    private void initShareInfo() {
        info = new ShareInfo();
        info.setWebUrl("https://www.baidu.com/?tn=39042058_14_oem_dg");
        info.setContent("分享类容");
        info.setTitle("分享标题");
        info.setThumb(R.drawable.default_img);
        info.setBitmap(shareDialog.getImageBitmap());
    }


    private void initData() {
        iconResIDs = new int[]{R.drawable.icon_gold, R.drawable.icon_gradle, R.drawable.icon_feedback,
                R.drawable.icon_setting};
        itemTitles = getResources().getStringArray(R.array.fragment_mine_item);
        itemHints = getResources().getStringArray(R.array.fragment_mine_hint);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ivShare:
                if (shareDialog == null) {
                    shareDialog = new ShareDialog(getContext(), R.style.Dialog, true);
                    shareDialog.setOnItemClick(this);
                    shareDialog.show();
                } else {
                    shareDialog.show();
                }
                break;
            case R.id.ivMessage:
                jumpToActivity(MyMessageActivity.class);
                break;
            case R.id.ivEdite:
                jumpToActivity(MyInfoActivity.class);
                break;
            case R.id.llSina:
                initShareInfo();
                ShareUtils.shareToSocialImage(mActivity, SHARE_MEDIA.SINA, info);
                shareDialog.hide();
                break;
            case R.id.llWeChat:
                initShareInfo();
                ShareUtils.shareToSocialImage(mActivity, SHARE_MEDIA.WEIXIN, info);
                shareDialog.hide();
                break;
            case R.id.llFriend:
                initShareInfo();
                ShareUtils.shareToSocialImage(mActivity, SHARE_MEDIA.WEIXIN_CIRCLE, info);
                shareDialog.hide();
                break;
            case R.id.llQQ:
                if (!((MainActivity) mActivity).checkPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE})) {
                    ((MainActivity) mActivity).requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x0002);
                    return;
                }
                initShareInfo();
                ShareUtils.shareToSocialImage(mActivity, SHARE_MEDIA.QQ, info);
                shareDialog.hide();
                break;
            case R.id.llGrade:
                if (UserProxy.getUser() == null) return;
                intent.setClass(mActivity, WebViewActivity.class);
                String url = UserProxy.getUser().getUserInfo().getMyLvUrl();

                intent.putExtra("url", url);
                jumpToActivity(intent);
                break;
            case R.id.llSetting:
                jumpToActivity(MySettingActivity.class);
                break;
            case R.id.llGold:
                jumpToActivity(RechargeActivity.class);
                break;
            case R.id.llFeedback:
                jumpToActivity(FeedbackActivity.class);
                break;
            case R.id.llFans:
                jumpToActivity(MyFansActivity.class);
                break;
            case R.id.llFollowed:
                jumpToActivity(MyAttentionActivity.class);
                break;
            case R.id.llCharm:
                intent.setClass(mActivity, WebViewActivity.class);
                intent.putExtra("url", UserProxy.getUser().getUserInfo().getCharmUrl());
                jumpToActivity(intent);
                break;
            case R.id.llInviteFriends:
                intent.setClass(mActivity, WebViewActivity.class);
                intent.putExtra("url", user.getUserInfo().getInviteUrl());
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onDestroy() {
        if (shareDialog != null)
            shareDialog.dismiss();
        shareDialog = null;
        UserProxy.removeListener(this);
        Observerable.getInstance().unSubscribe(ObserverableType.ATTENTION, this);
        super.onDestroy();
    }

    @Override
    public void onUserChange(User user) {
        this.user = user;
        if (user != null)
            setData();
    }

    @Override
    public <T> void update(ObserverableType key, T data) {
        int followed = Integer.parseInt(tvFollowed.getText().toString());
        followed += (Integer) data;
        if (followed < 0) followed = 0;
        tvFollowed.setText("" + followed);
    }

    @Override
    public void setPresenter(MineContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showUserInfo(UserInfo info) {
        if (info == null) return;
        user = UserProxy.getUser();
        user.setUserInfo(info);
        setData();
        UserProxy.setUser(user);
    }

    public void getUserInfo() {
        if (presenter != null) {
            presenter.getUserInfo();
        }
    }
}
